#include <iostream>
#include <pthread.h>

// 首先封装mutex
class Mutex
{
public:
    Mutex()
    {
        pthread_mutex_init(&mutex, nullptr);
    }
    void Lock()
    {
        pthread_mutex_lock(&mutex);
    }
    void Unlock()
    {
        pthread_mutex_unlock(&mutex);
    }
    ~Mutex()
    {
        pthread_mutex_destroy(&mutex);
    }

private:
    pthread_mutex_t mutex;
};

// 基于RAII实现锁
class LockGuard
{
public:
    LockGuard(Mutex *mutex)
        : mutex_(mutex)
    {
        mutex_->Lock();
    }
    ~LockGuard()
    {
        mutex_->Unlock();
    }
    Mutex *mutex_;
};